// ws_micro_shop/pages/rider/index.js
const api = require('../../../ws_micro_shop/utils/api.js');
const app = getApp();
let interval = null;
Page({
  data: {
    imgUrl: app.imgUrl,
  },

  onLoad(options) {
    let that = this;
    wx.getSetting({
      success(res) {
        if (typeof res.authSetting['scope.userLocation'] === 'undefined') {
          wx.getLocation({
            success: function(res) {
              that.setData({
                latitude: res.latitude,
                longitude: res.longitude,
              });
            },
            fail: function() {
              wx.navigateBack();
            },
          });
        } else if (res.authSetting['scope.userLocation'] === false) {
          that.setData({
            open_setting: true,
          });
        } else {
          wx.getLocation({
            success: function(res) {
              that.setData({
                latitude: res.latitude,
                longitude: res.longitude,
              });
            },
          });
        }
      },
    });
  },

  onShow() {
    this.getRiderInfo();
  },

  getRiderInfo() {
    let that = this;
    app.ws_micro_shoprequest({
      url: api.rider.index,
      success: function(res) {
        res.data && res.data.info && that.setData(res.data.info);
        if (res.code === 0) {
          let sec = res.data.info.rider_uplocation_hz || 0;
          if (res.data.info.is_online) {
            that.reportLocation(sec);
          } else {
            interval && clearInterval(interval);
          }
        }
      },
    });
  },

  reportLocation(sec) {
    let that = this;
    interval && clearInterval(interval);
    function reportFun() {
    app.ws_micro_shoprequest({
      url: api.rider.reportLocation,
      data: {
        lat: that.data.latitude,
        lng: that.data.longitude,
      },
      success: function(res) {},
    });
    }
    reportFun();
    interval = setInterval(function() {
      reportFun();
    }, sec * 1000);
  },

  work() {
    let that = this;
    let { is_online, rider_uplocation_hz } = this.data;
    wx.showModal({
      title: '提示',
      content: is_online ? '下班路上注意安全' : '确认打卡上班',
      confirmText: '确认',
      success: function(res) {
        if (res.confirm) {
          app.ws_micro_shoprequest({
            url: api.rider.work,
            data: {
              type: is_online ? 2 : 1,
            },
            success: function(res) {
              if (is_online) {
                that.reportLocation(rider_uplocation_hz * 1000);
              } else {
                interval && clearInterval(interval);
              }
              if (res.code === 0) {
                // 骑手上线id
                that.setData({
                  is_online: !is_online,
                });

                wx.showToast({
                  title: '打卡成功',
                  icon: 'none',
                  duration: 1500,
                  mask: false,
                });
              } else {
                wx.showToast({
                  title: res.msg,
                  icon: 'none',
                  duration: 1500,
                  mask: false,
                });
              }
            },
          });
        }
      },
    });
  },
});
